package algotithm.leetcode.bytedance.test1;

import java.util.HashMap;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/23-21:43
 * @URL https://leetcode-cn.com/problems/two-sum/
 * @Title 1. 两数之和
 */
public class Solution {

    public static void main(String[] args) {
        int[] nums = {3,2,4};
        int target = 6;
        Solution solution = new Solution();
        int[] twoSum = solution.twoSum(nums, target);
        for (int num : twoSum) {
            System.out.print(num + " ");
        }
    }

    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap<>(100);
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{i,map.get(target - nums[i])};
            }
            map.put(nums[i],i);
        }
        return new int[]{};
    }
}